package com.huang.controller;

import com.huang.pojo.*;
import com.huang.service.StudentService;
import com.huang.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ApplyController {

    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    @Autowired
    @Qualifier("TeacherServiceImpl")
    private TeacherService teacherService;

    //学生申请学分
    @RequestMapping("/student/toApplyScore")
    public String toApplyScore(ApplyScore applyScore, Model model,HttpSession session){
        model.addAttribute("applyScore",applyScore);

        return "studentApplyScore";
    }

    @RequestMapping("/student/applyScore")
    public String applyScore(ApplyScore applyScore, Model model,HttpSession session){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format( now );
        applyScore.setApplyTime(time);
        applyScore.setAnswerStatus("T");
        System.out.println("学生申请学分"+applyScore);
        if(studentService.applyScore(applyScore)!=0 && teacherService.updateAnswerStatus(applyScore)!=0){

            model.addAttribute("rightApplyMsg","您已成功提交申请，正在等待老师审核");

            //***************************************************************************************************
            //添加学生活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("申请学分"+applyScore.getActivityNo()+"提交成功");

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Student studentRecord= (Student) session.getAttribute("studentData");
            userDiary.setUserNo(studentRecord.getStudentNo());

            String stuLoginTime= (String) session.getAttribute("stuLoginTime");
            userDiary.setUserLoginTime(stuLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("申请学分"+applyScore.getActivityNo()+"提交成功已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

        }else {
            model.addAttribute("errorApplyMsg","您未能成功提交申请，请重试");
        }
        return "studentApplyScore";
    }

    //学生查看待审核的活动
    @RequestMapping("/student/toReviewActivity")
    public String toReviewActivity(Student student,Model model,HttpSession session){
        if(student.getStudentNo()==null){
            student= (Student) session.getAttribute("studentData");
        }
        List<Activity> list=studentService.reviewActivity(student);
        model.addAttribute("tmpStudent",student);
        model.addAttribute("list",list);

        return "studentViewReviewActivity";
    }

    //学生查看审核通过的活动
    @RequestMapping("/student/toPassActivity")
    public String toPassActivity(Student student,Model model,HttpSession session){
        List<Activity> list=studentService.passActivity(student);
        model.addAttribute("list",list);
        model.addAttribute("tmpStudent",student);


        return "studentPassActivity";
    }

    //老师查看待审核活动
    @RequestMapping("/teacher/toViewApply")
    public String toViewApply(ApplyScore applyScore,Model model){
        List<ApplyScore> list=teacherService.viewApply(applyScore);

        model.addAttribute("list",list);
        return "teacherViewApply";
    }

    //老师同意申请并留言
    @RequestMapping("/teacher/toApproveApply")
    public String toApproveApply(Message message,Model model,HttpSession session){
        Teacher teacher=new Teacher();
        teacher.setTeacherNo(message.getTeacherNo());
        message.setTeacherName(teacherService.findTeacherByNo(teacher).getTeacherName());
        model.addAttribute("message",message);

        return "teacherApproveApply";
    }

    @RequestMapping("/teacher/ApproveApply")
    public String ApproveApply(Message message, Model model,HttpSession session){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format( now );
        message.setAnswerTime(time);
        message.setApplyCount(teacherService.applyCount(message).size()+1);
        message.setApplyStatus("T");

        ApplyScore applyScore=new ApplyScore();
        applyScore.setActivityNo(message.getActivityNo());
        applyScore.setStudentNo(message.getStudentNo());
        applyScore.setApplyStatus("T");
        applyScore.setAnswerStatus("T");

        //处理学分事项
        StudentScore studentScore=new StudentScore();
        studentScore.setActivityNo(message.getActivityNo());
        studentScore.setStudentNo(message.getStudentNo());
        studentScore.setActivityName(message.getActivityName());
        studentScore.setStudentName(message.getStudentName());
        studentScore.setScoreTime(time);
        studentScore.setTeacherNo(message.getTeacherNo());
        studentScore.setTeacherName(message.getTeacherName());
        //根据活动编号查活动
        studentScore.setActivityScore(teacherService.queryActivityByNo(message.getActivityNo()).getActivityScore());

        if(teacherService.approveApply(message)!=0 && teacherService.updateStatue(applyScore)!=0&&teacherService.addStuScore(studentScore)!=0){
            model.addAttribute("applyMsg","留言成功，已同意审核");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("老师同意"+message.getStudentNo()+"申请并留言");

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("老师同意"+message.getStudentNo()+"申请并留言已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "teacherApproveApply";
        }else {
            model.addAttribute("applyMsg","出现错误未通过审核");
            return "teacherApproveApply";
        }
    }

    //老师查看已经审核通过的活动
    @RequestMapping("/teacher/toPassApply")
    public String toPassApply (Teacher teacher,Model model){
        List<ApplyScore> list=teacherService.viewPassApply(teacher);
        model.addAttribute("list",list);
        return "teacherViewPassApply";
    }

    //老师查看通过审核活动详情
    @RequestMapping("/teacher/passApplyMsg")
    public String passApplyMsg(Message message,Model model){

        List<Message> list=teacherService.applyCount(message);
        model.addAttribute("list",list);

        return "teacherViewPassApplyMsg";
    }

    //查看未通过审核的活动(与老师查看已经审核通过的活动103公用一个界面)
    @RequestMapping("/teacher/toUnPassApply")
    public String toUnPassApply (Teacher teacher,Model model){
        List<ApplyScore> list=teacherService.viewUnPassApply(teacher);
        model.addAttribute("list",list);
        return "teacherViewPassApply";
    }

    //老师查看未能通过审核活动详情(与老师查看通过审核活动详情112公用一个界面)
    @RequestMapping("/teacher/unPassApplyMsg")
    public String unPassApplyMsg(Message message,Model model){
        List<Message> list=teacherService.applyCount(message);
        model.addAttribute("list",list);
        return "teacherViewPassApplyMsg";
    }


    //老师不同意申请并留言
    @RequestMapping("/teacher/toUnApproveApply")
    public String toUnApproveApply(Message message,Model model){
        Teacher teacher=new Teacher();
        teacher.setTeacherNo(message.getTeacherNo());
        message.setTeacherName(teacherService.findTeacherByNo(teacher).getTeacherName());
        model.addAttribute("message",message);
        return "teacherUnApproveApply";
    }

    @RequestMapping("/teacher/unApproveApply")
    public String unApproveApply(Message message, Model model,HttpSession session){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        message.setAnswerTime(time);
        message.setApplyCount(teacherService.applyCount(message).size()+1);
        message.setApplyStatus("F");

        ApplyScore applyScore=new ApplyScore();
        applyScore.setActivityNo(message.getActivityNo());
        applyScore.setStudentNo(message.getStudentNo());
        applyScore.setApplyStatus("F");
        applyScore.setAnswerStatus("F");

        if(teacherService.approveApply(message)!=0 && teacherService.updateStatue(applyScore)!=0 && teacherService.updateAnswerStatus(applyScore)!=0){
            model.addAttribute("applyMsg","留言成功，已拒绝审核");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("老师不同意"+message.getStudentNo()+"申请并留言");

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("老师不同意"+message.getStudentNo()+"申请并留言已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "teacherUnApproveApply";
        }else {
            model.addAttribute("applyMsg","出现错误未通过审核");
            return "teacherUnApproveApply";
        }
    }

    //学生查看自己的审核未通过
    @RequestMapping("/student/toUnPassApply")
    public String toUnPassApply(Student student,Model model,HttpSession session){
        List<Activity> list=studentService.showUnPassApply(student);
        model.addAttribute("list",list);
        model.addAttribute("tmpStudent",student);


        return "studentUnPassActivity";
    }

    //学生撤销正在申请的活动
    @RequestMapping("/student/toRemoveApplyStu")
    public String toRemoveApplyStu(ApplyScore applyScore, Model model, HttpSession session){
        Student student= (Student) session.getAttribute("studentData");
        applyScore.setStudentNo(student.getStudentNo());
        System.out.println("学生撤销正在申请的活动"+applyScore);
        if(studentService.removeApplyStu(applyScore)!=0){
            model.addAttribute("removeApplyRightMsg","成功撤销");

            //***************************************************************************************************
            //添加学生活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("成功撤销"+applyScore.getActivityNo()+"活动的申请");

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Student studentRecord= (Student) session.getAttribute("studentData");
            userDiary.setUserNo(studentRecord.getStudentNo());

            String stuLoginTime= (String) session.getAttribute("stuLoginTime");
            userDiary.setUserLoginTime(stuLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("成功撤销"+applyScore.getActivityNo()+"活动的申请已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/student/toReviewActivity";
        }else {
            model.addAttribute("removeApplyRightMsg","撤销失败");

            //***************************************************************************************************
            //添加学生活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("未能成功撤销"+applyScore.getActivityNo()+"活动的申请");

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Student studentRecord= (Student) session.getAttribute("studentData");
            userDiary.setUserNo(studentRecord.getStudentNo());

            String stuLoginTime= (String) session.getAttribute("stuLoginTime");
            userDiary.setUserLoginTime(stuLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("未能成功撤销"+applyScore.getActivityNo()+"活动的申请已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/student/toReviewActivity";
        }
    }

    //学生查看自己的申请活动中的备注信息
    @RequestMapping("/student/toShowSelfApplyMsgStu")
    public String toShowSelfApplyMsgStu(ApplyScore applyScore,Model model,HttpSession session){

        List<ApplyScore> list=studentService.showSelfApplyMsgStu(applyScore);
        model.addAttribute("list",list);

        return "studentShowSelfApplyMsg";
    }

    //学生查看申请资料中转
    @RequestMapping("/student/toViewAnnex")
    public String toViewAnnex(ApplyScore applyScore,Model model){

        System.out.println("学生查看申请资料中转1"+applyScore);
        applyScore=studentService.findApplyByNoAndTime(applyScore);
        System.out.println("学生查看申请资料中转2"+applyScore);

        if(applyScore.getApplyAnnexName()==null){
            model.addAttribute("annexMsg","无附件");

            //加载必要信息
            Message message=new Message();
            message.setStudentNo(applyScore.getStudentNo());
            message.setActivityNo(applyScore.getActivityNo());
            List<Message> list=teacherService.applyCount(message);
            model.addAttribute("list",list);

            return "teacherViewPassApplyMsg";
        }

        return "redirect:/user/downloadApplyAnnex?activityNo="+applyScore.getActivityNo()+"&applyAnnexName="+applyScore.getApplyAnnexName();

    }

}
